package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags= "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 增加套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("增加套餐信息")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("增加套餐信息:{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 分页查询信息
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询信息")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询参数:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 更改套餐状态信息
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更改套餐状态信息")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("需要更改的状态：{}，需要更改的id：{}", status, id);
        setmealService.update(status, id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        setmealService.delBatch(ids);
        return Result.success();
    }

    /**
     * 根据套餐id返回套餐信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id返回套餐信息")
    public Result<SetmealVO> getBySetmealId(@PathVariable Long id) {
        log.info("套餐id：{}", id);
        SetmealVO setmealVO = setmealService.getBySetmealId(id);
        return Result.success(setmealVO);
    }

    /**
     * 更新套餐信息
     * @return
     */
    @PutMapping
    @ApiOperation("更新套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
     log.info("更改套餐信息:{}", setmealDTO);
     setmealService.updateSetmeal(setmealDTO);
     return Result.success();
    }
}
